<?php

class SettingsController extends Controller
{
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	
	public function accessRules()
	{
		return array(
			array('allow',  // allow authenticated user  to perform 'index','detail','editindex'
				'actions'=>array('index','detail','edit'),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
        
        public function actionIndex()
	{
		
		$pid = Yii::app()->request->getParam('pid');
		if($pid==NULL)
		  throw new CHttpException(404,'Project not found');
		$detail = $this->createUrl('/settings/detail',array('pid'=>$pid));
		$edit = $this->createUrl('/settings/edit',array('pid'=>$pid));
		$model=$this->loadModel($pid);
		//find User object of porject manager
		$ProjectManager= User::model()->findByPk($model->project_manager_id);
		$ProgrammingLanguage= ProgrammingLanguage::model()->find('programming_language_id',$model-> project_programming_language_id);
        //find Project Catagory
        $ProjectCatagory= ProjectCategory::model()->findByPk($model->project_category_id); 
		$this->render('index', array('model'=>$model,'ProjectManager'=>$ProjectManager,'ProgrammingLanguage'=>$ProgrammingLanguage,
		'ProjectCatagory'=>$ProjectCatagory,'edit'=>$edit));
	}
/*
	public function actionDetail(){
		$ProgrammingLanguage = new ProgrammingLanguage();
		$pid=Yii::app()->request->getParam('pid');
		if($pid==NULL)
		  throw new CHttpException(404,'Project not found');
		$index = $this->createUrl('/settings/index',array('pid'=>$pid));
		$editDetail = $this->createUrl('/settings/editDetail',array('pid'=>$pid));
		//Load Project Model
		$model=$this->loadModel($pid);
		//find User object of porject manager
		$ProjectManager= User::model()->findByPk($model->project_manager_id);
		$ProgrammingLanguage= ProgrammingLanguage::model()->find('programming_language_id',$model-> project_programming_language_id);
        //find Project Catagory
        $ProjectCatagory= ProjectCategory::model()->findByPk($model->project_category_id); 
		$this->render('detail', array('model'=>$model,'ProjectManager'=>$ProjectManager,'ProgrammingLanguage'=>$ProgrammingLanguage,
		'ProjectCatagory'=>$ProjectCatagory,'index' => $index, 'editDetail'=>$editDetail));
	}
	/**
	 * Action edit project general information
	 * FOrm id "EditIndexForm" 
	 */
	public function actionEdit() {
		$pid=Yii::app()->request->getParam('pid');
		$backToIndex=$this->createUrl('settings/index',array('pid'=>$pid));
		if($pid==NULL)
		  throw new CHttpException(404,'Project not found');
		$index=$this->createUrl('/settings/index',array('pid'=>$pid));
		//get information
		$model=$this->loadModel($pid);
	
		if(isset($_POST['ajax'])&& $_POST['ajax']==='editIndex-form'){
		echo CActiveForm::validate($model);
		Yii::app()->end();
		}
		$ProgrammingLanguage = ProgrammingLanguage::model()->findAll(array('order'=>'programming_language_id'));
		   $data = CHtml::listData($ProgrammingLanguage, 'programming_language_id', 'programming_language_name');
		   $currency = $model->getListCurrency();
		 /** Lay ra danh sach thanh vien du an*/
		 $member= $model->getMemberList();
         $memberList= CHtml::listData($member, 'user_id', 'user_name');
		/**
		 * Lay ra danh sach cac loai du an
		 * */
         $ProjectCategoryList= ProjectCategory::model()->findAll(array('order'=>'project_category_id'));
         $ProjectCategory=CHtml::listData($ProjectCategoryList,'project_category_id','project_category_name');
		//collect user Input Data
		if(isset($_POST['Project'])){
			//if($_POST['Project']['image']==NULL)$_POST['Project']['image']= $model->project_logo;
			$model->attributes= $_POST['Project'];
			
			$image = CUploadedFile::getInstance($model, 'image');
			if(is_object($image)){
				Yii::log("***Chung:vao cho save anh");
				$image->saveAs(Yii::app()->basePath.'/images/project/logo/'.$image->name);
				$model->project_logo = Yii::app()->request->baseUrl."/protected/images/project/logo/".$image->name;	
			}

			Yii::log("***truoc khi convert: ".$model->project_start_date."--".$model->project_end_date);
			     
			    $startDate= Html::formatDateTime($model->project_start_date, 'MM/dd/yyyy', 'Y-m-d h:m:s');
			    $model->project_start_date = $startDate;
			    $model->project_end_date = Html::formatDateTime($model->project_end_date,'MM/dd/yyyy','Y-m-d h:m:s');
			    
			    Yii::log("***sau khi convert: ".$model->project_start_date."--".$model->project_end_date);
                  Yii::log('********* project logo :'.$model->project_logo);
            
           
			if ($model->save()){
			  Yii::log("***Chung: save thanh cong ");
			 $this->redirect(array('settings/index','pid'=>$pid));
			}
			}
		$this->render('edit', array('model'=> $model,'data'=>$data,
		'memberList'=>$memberList,'ProjectCategory'=>$ProjectCategory,
		'currency'=>$currency,'backToIndex'=>$backToIndex));
		
	}
	/*
	public function actionEditDetail() {
		$pid=Yii::app()->request->getParam('pid');
		if($pid==NULL)
		  throw new CHttpException(404,'Project not found');
		$index = $this->createUrl('/settings/index',array('pid'=>$pid));
		$detail = $this->createUrl('/settings/detail',array('pid'=>$pid));
		 /** Form 
		  * Lay thong tin cu ra 
		$model=$this->loadModel($pid);
		
         
		if(isset($_POST['Project'])){
		 $model->attributes=$_POST['Project'];
		 if ($model->save())
			  $this->redirect($detail);
		}
		$this->render('editDetail', array('detail' => $detail, 
		'index' => $index,'model'=>$model,'data'=>$data,
		'memberList'=>$memberList,'ProjectCategory'=>$ProjectCategory));
		
	}
	*/
	public function actionTest()
	{
		$this->render('test');
	}
	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 
	public function actionCreate()
	{
		$model=new Project;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Project']))
		{
			$model->attributes=$_POST['Project'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->project_id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Project']))
		{
			$model->attributes=$_POST['Project'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->project_id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 

	/**
	 * Manages all models.
	 
	public function actionAdmin()
	{
		$model=new Project('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Project']))
			$model->attributes=$_GET['Project'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Project::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='project-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
//	public $layout='//layouts/column2';

	

	
	
	
}
